<template>
  <div>
    <!-- 面包屑导航条 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :replace="true" :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索、添加操作 -->
      <el-row :gutter="13">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @clear="getuserlist()">
            <el-button slot="append" icon="el-icon-search" @click="getuserlist()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="success" @click="newlist()" round>查询全部数据</el-button>
        </el-col>
      </el-row>
      <!-- 数据面板 -->
      <el-table :data="tableDate" stripe border style="width: 100%">
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="username" label="用户名">
        </el-table-column>
        <el-table-column prop="email" label="邮箱">
        </el-table-column>
        <el-table-column prop="role" label="角色名称">
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
        </el-table-column>
        <el-table-column label="状态">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-switch v-model="scope.row.user_state" @change="statechange(scope.row)" active-color="#13ce66" inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑用户" placement="top">
              <el-button type="primary" icon="el-icon-edit" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="提升权限" placement="top">
              <el-button type="warning" icon="el-icon-user" circle @click="upRights(scope.row._id)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除用户" placement="top">
              <el-button type="danger" icon="el-icon-delete" circle @click="removeUser(scope.row._id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryinfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableDate: [],
      queryinfo: {
        query: '',
        pageNum: 1,
        pageSize: 5,
        levels: 1
      },
      total: 0
    }
  },
  created() {
    this.getuserlist()
  },
  methods: {
    async getuserlist() {
      var ret = await this.$http.get('/users', { params: this.queryinfo })
      if (ret.data.err_code !== 0) {
        console.log('获取用户列表失败')
      }
      console.log(ret.data)
      this.tableDate = ret.data.data
      this.total = ret.data.total
    },
    async statechange(userinfo) {
      var ret = await this.$http.put(`/users/${userinfo._id}/state/${userinfo.user_state}`)
      if (ret.data.err_code !== 0) {
        this.$message({
          message: '修改用户状态失败',
          type: 'error'
        })
      }
      this.$message({
        message: '修改用户状态成功',
        type: 'success'
      })
    },
    newlist() {
      this.queryinfo.query = ''
      this.getuserlist()
    },
    handleSizeChange(newpagesize) {
      this.queryinfo.pageSize = newpagesize
      this.getuserlist()
    },
    handleCurrentChange(newpagenum) {
      this.queryinfo.pageNum = newpagenum
      this.getuserlist()
    },
    removeUser(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        var ret = await this.$http.delete(`/users/delete/${id}`)
        if (ret.data.err_code !== 0) {
          this.$message.error('删除失败')
        }
        this.getuserlist()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async upRights(id) {
      var ret = await this.$http.get(`/users/upRights/${id}`)
      if (ret.data.err_code !== 0) {
        this.$message.error('提升权限失败')
      }
      this.getuserlist()
      this.$message({
        type: 'success',
        message: '用户已晋级为管理员!'
      })
    }
  }
}

</script>
<style>
.el-breadcrumb {
  margin-bottom: 15px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 0px;
}

.el-row {
  margin-bottom: 20px;
}

.el-card {
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.15) !important;
}

.el-pagination {
  margin-top: 20px;
}

</style>
